

chrome.runtime.onMessage.addListener(async (msg, sender) => {
  console.log(msg)
  for(var i=0;i<msg['data']['list'].length;i++){
    var id = msg['data']['list'][i]['spu_id'];
    $.ajax({
      url: "https://www.goat.com/web-api/v1/product_variants/buy_bar_data?productTemplateId="+id+"&countryCode=US",
      success: (function(i){
        return function(data){
            var body = JSON.stringify(data);
            id = msg['data']['list'][i]['spu_id'];
            console.log(id);
            console.log(body);
            var type = "goat";
            chrome.runtime.sendMessage({ data: {type, id,body }, close: false })
        }
      })(i),
      error: function(){
        alert(i); //这样使用必然返回i=3
      }
    });
  }
})




function getData() {
  var body = document.body.innerText;

    var t={
       getQueryString:function(name){
          var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
          var r = window.location.search.substr(1).match(reg);
          if(r!=null)
            return  decodeURI(r[2]);
            return null;
       }
   }
    //调用
    var id=t.getQueryString("productTemplateId");//城市名称
    console.log('getData 成功！id:'+id + " body"+body);
    chrome.runtime.sendMessage({ data: { id,body }, close: true })
}


